﻿using System;
using System.Xml;
using System.IO;

namespace Vessel_calculation.Classes
{
	/// <summary>
	/// Class for the reading xml file.
	/// </summary>
	public class ReadFile
	{	
		public double dL, dT;
		public int waterlines, bends;
		public Point<double>[,] drawing;
		public double[] xf, xa;
		
		private string patch;
		
		public ReadFile(string patch)
		{		
			this.patch = patch;			
			
			XmlDocument draw = new XmlDocument();
			draw.Load(patch);
			
			XmlNode dr;
									
			dr = draw.SelectSingleNode("Drawing/dL");
			this.dL = Convert.ToDouble(dr.InnerText);
			
			dr = draw.SelectSingleNode("Drawing/dT");
			this.dT = Convert.ToDouble(dr.InnerText);
			
			dr = draw.SelectSingleNode("Drawing/Number_of_waterlines");
			this.waterlines = Convert.ToInt32(dr.InnerText);
			
			dr = draw.SelectSingleNode("Drawing/Number_of_bends");
			this.bends = Convert.ToInt32(dr.InnerText);
			
			int l = 1;
			
			dr = draw.SelectSingleNode("Drawing/Deck_coordinates");
			if (dr.InnerText == "Yes") {
				this.waterlines++;
				l++;
			}
			
			drawing = new Point<double>[this.bends, this.waterlines];
			
			for (int j=0; j<=(waterlines-l); j++)
			{
				for (int i=0; i<=(bends-1); i++)
				{
					dr = draw.SelectSingleNode("Drawing/Waterlines/WL" + Convert.ToString(j)
					                           + "/B" + Convert.ToString(i));
					drawing[i, j] = new Point<double>(dL*(this.bends-1)/2-dL*i, 
					                          Convert.ToDouble(dr.InnerText),
							                  dT*(j));
				}
			}
			
			dr = draw.SelectSingleNode("Drawing/Deck_coordinates");
			if (dr.InnerText == "Yes") {
			int k=waterlines-1;
			for (int i=0; i<=(bends-1); i++)
			{
				dr = draw.SelectSingleNode("Drawing/Deck/Waterline/B" + Convert.ToString(i));
				drawing[i, k] = new Point<double>(dL*(this.bends-1)/2-dL*i, 
					                      Convert.ToDouble(dr.InnerText),
							              0);				
			}
			
			for (int i=0; i<=(bends-1); i++)
			{
				dr = draw.SelectSingleNode("Drawing/Deck/Z/B" + Convert.ToString(i));
				drawing[i, k].z = Convert.ToDouble(dr.InnerText);				
			}
			}
			
			xf = new double[waterlines];
			xa = new double[waterlines];
			
			for (int j=0; j<=(waterlines-1); j++)
			{
				dr = draw.SelectSingleNode("Drawing/Additional_data/Xf/WL" + Convert.ToString(j));
				xf[j] = Convert.ToDouble(dr.InnerText);
			}
			
			for (int j=0; j<=(waterlines-1); j++)
			{
				dr = draw.SelectSingleNode("Drawing/Additional_data/Xa/WL" + Convert.ToString(j));
				xa[j] = Convert.ToDouble(dr.InnerText);
			}
			
		}
		
	}
}
